<?php


namespace App\Http\Controllers\Admin;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

//对公账号
class CorporateAccountController extends BaseController
{
    /**
     * @Desc:列表
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function list(Request $request)
    {
        $param = $request->only('page', 'page_size', 'user_code', 'bank_num', 'mobile');
        $v = app('validator')->make($param, [
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1',
            'user_code' => 'filled|integer',
            'bank_num' => 'filled|string',
            'mobile' => 'filled|string'
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_corporate_account')->list($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:详情
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function info(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_bank,id',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_corporate_account')->info($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:新增
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function create(Request $request)
    {
        $param = $request->only([
            'user_code',            //用户编号
            'cardholder',           //公司名称
            'bank_num',             //卡号
            'bank_zhi',            //开户行支行
            'mobile',               //开户手机号
            'nickname',             //昵称
        ]);
        $v = app('validator')->make($param, [
            'user_code' => 'required|integer|min:0',
            'cardholder' => 'required|string|max:50',
            'bank_num' => 'required|string|max:30|min:6',
            'bank_zhi' => 'required|string|max:255',
            'mobile' => 'required|string|max:11|regex:/^1[3456789]\d{9}$/',
            'nickname' => 'required|string|max:255'
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_corporate_account')->create($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:更新
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function update(Request $request)
    {
        $param = $request->only([
            'id',
            'user_code',            //用户编号
            'cardholder',           //公司名称
            'bank_num',             //卡号
            'bank_zhi',            //开户行支行
            'mobile',               //开户手机号
            'nickname',             //昵称
        ]);
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_bank,id',
            'user_code' => 'required|integer|min:0',
            'cardholder' => 'required|string|max:50',
            'bank_num' => 'required|string|max:30|min:6',
            'bank_zhi' => 'required|string|max:255',
            'mobile' => 'required|string|max:11|regex:/^1[3456789]\d{9}$/',
            'nickname' => 'required|string|max:255'
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_corporate_account')->update($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:删除
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function delete(Request $request)
    {
        $param = $request->only(['ids']);
        $v = app('validator')->make($param, [
            'ids' => 'required|array',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_corporate_account')->delete($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:根据用户编号获取用户昵称
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/10/8 16:25
     */
    public function userInfo(Request $request)
    {
        $param = $request->only(['user_code']);
        $v = app('validator')->make($param, [
            'user_code' => 'required|string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $res = app('repo_corporate_account')->userInfo($param);
        return Response::sendData($res);
    }
}